import 'package:flutter/material.dart';
import 'package:gold/ui/my/order/recover/online_list_page.dart';
import 'package:gold/ui/my/order/recover/recover_config.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/xfg_text.dart';
import 'package:scroll_to_index/scroll_to_index.dart';

/// @description: 线上订单页面
/// @author tangzhentao
/// @created at 21:44 2025/3/5


class OnlineOrderPage extends StatefulWidget {
  const OnlineOrderPage({super.key});

  @override
  State<StatefulWidget> createState() => _OnlineOrderState();
}

class _OnlineOrderState extends State<OnlineOrderPage> with AutomaticKeepAliveClientMixin {
  /// 状态控制器
  final ValueNotifier<String> _statusNotifier = ValueNotifier(recoverStatusList.first);

  /// 滚动到指定位置
  final AutoScrollController _scrollController = AutoScrollController();

  /// 状态页面控制器
  final PageController _controller = PageController(initialPage: 0);


  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      color: XfgColor.background,
      child: Column(
        children: [
          Container(
            height: XfgSize().size52,
            color: XfgColor.white,
            child: ValueListenableBuilder<String>(
                valueListenable: _statusNotifier,
                builder: (_, status, __) => ListView.builder(
                    scrollDirection: Axis.horizontal,
                    controller: _scrollController,
                    itemCount: recoverStatusList.length,
                    itemBuilder: (_, index) {
                      final current = recoverStatusList[index];
                      final isSelect = status == current;
                      return AutoScrollTag(
                        key: ValueKey(index),
                        controller: _scrollController,
                        index: index,
                        child: GestureDetector(
                          onTap: () {
                            if (!isSelect) {
                              _statusNotifier.value = status;
                              _controller.jumpToPage(index);
                              _scrollController.scrollToIndex(index);
                            }
                          },
                          behavior: HitTestBehavior.opaque,
                          child: Padding(
                            padding: EdgeInsets.symmetric(horizontal: XfgSize().size12),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: [
                                XfgText(getRecoverOrderStatusString(current), fontSize: XfgSize().text14, color: XfgColor.text1, bold: isSelect ? FontWeight.bold:null),
                                SizedBox(height: XfgSize().size10),
                                Container(height: XfgSize().size3, width: XfgSize().size20, decoration: isSelect ? BoxDecoration(color: XfgColor.theme, borderRadius: BorderRadius.circular(XfgSize().size3)):null),
                                SizedBox(height: XfgSize().size5)
                              ],
                            ),
                          ),
                        ),
                      );
                    }
                )
            ),
          ),
          Expanded(
              child: PageView.builder(
                controller: _controller,
                onPageChanged: (index) {
                  _statusNotifier.value = recoverStatusList[index];
                  _scrollController.scrollToIndex(index);
                },
                itemCount: recoverStatusList.length,
                itemBuilder: (_, index) => OnlineListPage(status: recoverStatusList[index]),
              )
          )
        ],
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}